/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.elte.progelm.web.admin.main;

import hu.elte.progelm2.ejb.entity.Forrest;
import hu.elte.progelm2.ejb.entity.Treetypes;
import hu.elte.progelm2.ejb.simulator.SimulatorRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author ntb
 */
public class Register {

    private Context ctx;
    private SimulatorRemote simulator;

    public Register() {
        try {
            ctx = new InitialContext();
            simulator = (SimulatorRemote) ctx.lookup("Simulator/remote");
            populateForrests();
        } catch (NamingException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateForrests() {
        List<Forrest> forrestList = simulator.getAllForrest();
        forrests = new ArrayList();
        for (Forrest item : forrestList) {
            forrests.add(new SelectItem(item.getId(), item.getName()));
        }
    }

    public String SaveResult() {
        Treetypes tree = new Treetypes();
        tree.setDescription(description);
        tree.setForrestid(simulator.getForrestById(selectedForrest));
        tree.setName(treename);
        simulator.AddTree(tree);

        Reset();
        
        return null;
    }

    private void Reset(){
        this.description = "";
        this.treename = "";
    }

    private List<SelectItem> forrests;
    private String selectedForrest;
    private String treename;
    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTreename() {
        return treename;
    }

    public void setTreename(String treename) {
        this.treename = treename;
    }

    public String getSelectedForrest() {
        return selectedForrest;
    }

    public void setSelectedForrest(String selectedForrest) {
        this.selectedForrest = selectedForrest;
    }

    public List<SelectItem> getForrests() {
        return forrests;
    }

    public void setForrests(List<SelectItem> forrests) {
        this.forrests = forrests;
    }

}
